// pages/addDriveway/addDriveway.js
const host=getApp().globalData.host;
Page({

      /**
       * 页面的初始数据
       */
      data: {
            driveway:'',
            isFree:true,
            pay:''
      },
      /**判断是否能够提交 */
      isSubmit(){
            if(this.data.driveway==''){
                  wx.showToast({
                    title: '带*项必填',
                    icon:'error'
                  })
                  return;
            }
            if(!(this.data.isFree)&&this.data.pay==''){
                  wx.showToast({
                        title: '带*项必填',
                        icon:'error'
                  })
                      return;
            }
            this.submit();
      },
      submit(){
            wx.showLoading({
                  title: '发布共享信息',
            })
            let formData={
                  userId:wx.getStorageSync('userId'),
                  driveway:this.data.driveway,
                  isPay:this.data.isFree?'1':'2',
                  pay:this.data.pay,
                  contact:wx.getStorageSync('userInfo').phone,
                  communityId:wx.getStorageSync('userInfo').housingId
            }
            wx.request({
                  url: host+'/client/driveway/addInfo',
                  method:'POST',
                  header:{
                        'content-type': 'application/x-www-form-urlencoded'
                  },
                  data:formData,
                  success:res=>{
                        if(res.data.status.code==200){
                              wx.showToast({
                                    title: '发布成功'
                              })
                              wx.navigateBack({
                                    delta: 1,
                              })
                        }else if(res.data.status.code==203){
                              wx.showModal({
                                    title:'车位未记录',
                                    content:'是否前往个人信息页面补全车位信息？',
                                    success (res) {
                                          if (res.confirm) {
                                                wx.navigateTo({
                                                      url: '/pages/userInfo/userInfo',
                                                })
                                          } else if (res.cancel) {
                                                
                                          }
                                    }
                              })
                              wx.showToast({
                                    title: '车位未记录',
                                    icon:'error'
                              })
                        }
                  },
                  fail:()=>{
                        wx.showToast({
                              title: '服务器繁忙',
                              icon:'error'
                        })
                  }
            })
      },
      // 输入车位
      drivewayInput(e){
            this.setData({
                  driveway:e.detail.value
            })
      },
      isFreeChange(e){
            this.setData({
                  isFree:e.detail.value
            })
      },
      payInput(e){
            if(isNaN(e.detail.value)){
                  wx.showToast({
                        title: '需要数字',
                        icon:'error'
                  })
                  this.setData({
                        pay:''
                  })
                  return;
            }
            this.setData({
                  pay:e.detail.value
            })
      },
      // 发布的记录
      handleList(){
            wx.navigateTo({
                  url: '/pages/drivewayList/drivewayList',
            })
      }
})